package jhelp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.ResourceBundle;

import javax.swing.JFileChooser;

public class ClientListener extends KeyAdapter implements ActionListener{
	
	private View view;
	DBDialog dbDialog;
	private Controller controller;

    public ClientListener(Client client, Controller controller) {
        this.view = client;
        this.controller = controller;
    }

	public void actionPerformed(ActionEvent e) {
		switch (e.getActionCommand()){
			case "exit":
				view.close();
			break;
			case "openfile":
				JFileChooser chooseFileDialog = new JFileChooser("D://Dev//");
				chooseFileDialog.showOpenDialog(chooseFileDialog);
				File dataFile = chooseFileDialog.getSelectedFile();
				if(dataFile != null){
					String filePath = dataFile.getPath();
					controller.setModelDataSource(filePath, "file");
				}
			break;
			case "openxml":
				JFileChooser chooseXMLDialog = new JFileChooser("D://Dev//");
				chooseXMLDialog.showOpenDialog(chooseXMLDialog);
				File xmlFile = chooseXMLDialog.getSelectedFile();
				if(xmlFile != null){
					String filePath = xmlFile.getPath();
					controller.setModelDataSource(filePath, "xml");
				}
			break;
			case "find":
				controller.find();
			break;
			case "opendb":
				dbDialog = new DBDialog();
				dbDialog.setVisible(true);
				String dataSourcePath = "" + dbDialog.getDbURL() +
										"#" + dbDialog.getDbLogin() +
										"#" + dbDialog.getDbPassword() +
										"#";
				controller.setModelDataSource(dataSourcePath, "db");
				dbDialog.dispose();
			break;
			case "selectdb":
			break;
			case "dbselection_cancelled":
				dbDialog.dispose();
			break;
		}
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		switch (ke.getKeyCode()) {
		case KeyEvent.VK_ENTER :
			controller.find();
			break;
		case KeyEvent.VK_ESCAPE :
			view.close();
			break;
		}
	}

	
}
